﻿using System;
using System.Collections.Generic;
using System.Text;

namespace System
{
    using System.ComponentModel;
    using System.Reflection;

    public static class EnumHelper
    {

        /// <summary>
        /// 获取位域枚举的描述，多个按分隔符组合
        /// </summary>
        public static string GetDescriptions(this Enum @this, string separator = ",")
        {
            var names = @this.ToString().Split(',');
            string[] res = new string[names.Length];
            var type = @this.GetType();
            for (int i = 0; i < names.Length; i++)
            {
                var field = type.GetField(names[i].Trim());
                if (field == null) continue;
                res[i] = GetDescription(field);
            }
            return string.Join(separator, res);
        }

        private static string GetDescription(FieldInfo field)
        {
            var att = System.Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute), false);
            return att == null ? field.Name : ((DescriptionAttribute)att).Description;
        }

        /// <summary>
        /// 将枚举类型的值和它的描述转为字典
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static Dictionary<object, string> EnumAsDictionary(Type type)
        {
            if (!type.IsEnum)
            {
                throw new NotSupportedException("必须输入枚举的运行时类型。");
            }
            Dictionary<object, string> dic = new Dictionary<object, string>();
            var values = Enum.GetValues(type);
            foreach (object item in values)
            {
                var fieldName = Enum.GetName(type, item);
                dic.Add(item, GetDescription(type.GetField(fieldName)));
            }
            return dic;
        }
        /// <summary>
        /// 将枚举值和其描述作为字典返回
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static Dictionary<T, string> EnumAsDictionary<T>() where T : Enum
        {
            var t = typeof(T);
            Dictionary<T, string> dic = new Dictionary<T, string>();
            var values = Enum.GetValues(t);
            foreach (T item in values)
            {
                var fieldName = Enum.GetName(t, item);
                dic.Add(item, GetDescription(t.GetField(fieldName)));
            }
            return dic;
        }
        /// <summary>
        /// 获取枚举值的描述信息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetDescription<T>(T value)
        {
            var t = typeof(T);
            var fieldName = Enum.GetName(t, value);
            return GetDescription(t.GetField(fieldName));
        }
        public static string GetDescription(Type type, object value)
        {
            var fieldName = Enum.GetName(type, value);
            return GetDescription(type.GetField(fieldName));
        }
    }
}
